package com.pinc.cpbl;

import java.io.InputStream;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class show_grade extends WidgetActivity {
	private ListView lv;
	private Spinner sp;
	private Spinner spLeague;
	private int mLeague;
	private static final String[][] ARR_ITEM_NAME = {
		{"b_AVG", "b09", "b05", "b03", "b04", "b10", "b11", "b06", "b07", "b08", "b_G", "b01", "b02", "b13", "b14", "b15", "b16", "b15%2bb16%2bb17", "b18", "b19", "b20", "b_PSS", "b_OBP", "b_SLG", "b_TA", "b_SSI"},
		{"p061", "p_ERA", "p18", "p07", "p033", "p043", "p05", "p081", "p_WHIP", "p062", "p071", "p09+desc%2cp10", "p_G", "p021", "p_G-p021", "p11", "p12", "p13", "p14", "p15", "p16", "p17", "p15%2bp16%2bp17", "p19", "p20", "p21", "p22"}
	};
	private static final byte[][] ARR_ITEM_OFFSET = {
		{14, 11, 7, 5, 6, 12, 13, 8, 9, 10, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
		{8, 14, 9, 10, 5, 6, 7, 12, 15, 9, 11, 13, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13}
	};
	private static final byte[][] ARR_ITEM_PAGE = {
		{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
		{1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
	};
	private static final String[][] POS_WEBPAGE = {{"pbat_year.aspx", "ppitch_year.aspx"},{"pbat2_year.aspx", "ppitch2_year.aspx"}};
	private static final int[] POS_ARRAY_ID = {R.array.bgrade_array, R.array.pgrade_array};
	private static final int FIELDER = 0;
	private static final int PITCHER = 1;
	private byte mItem;
	private int mPosition;
	private String mItemStr;
	private int mPage;
	private ShowBGradeTask mTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_gradeview);

		switch (getIntent().getExtras().getInt("category"))
		{
		case 1:
			mPosition = PITCHER;
			break;
		default:
			mPosition = FIELDER;
		}

		mItem = -1;
		mLeague = 0;

		initWidget();

		ArrayAdapter<CharSequence> adapter;

		adapter =  ArrayAdapter.createFromResource(
				show_grade.this, POS_ARRAY_ID[mPosition], android.R.layout.simple_spinner_item);

		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp.setAdapter(adapter);
		sp.setOnItemSelectedListener(new MyOnItemSelectedListener());

		ArrayAdapter<CharSequence> adapter2;

		adapter2 =  ArrayAdapter.createFromResource(
				show_grade.this, R.array.league_array, android.R.layout.simple_spinner_item);

		adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spLeague.setAdapter(adapter2);
		spLeague.setOnItemSelectedListener(new LeagueSelectedListener());
	}

	@Override
	protected void initUpbar() {
		if ( mPosition == FIELDER)
			tv_title.setText(R.string.title_batter_rank);
		else
			tv_title.setText(R.string.title_pitcher_rank);

		b_right.setText(getResources().getString(R.string.str_query));
		b_right.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mTask = new ShowBGradeTask();
				mTask.execute(show_grade.this);
			}
		});

		mProgbar.setVisibility(View.INVISIBLE);
	}

	@Override
	protected void initWidget() {
		super.initWidget();
		lv = (ListView)findViewById(R.id.listView1);
		sp = (Spinner) this.findViewById(R.id.spinner1);
		spLeague = (Spinner) this.findViewById(R.id.spinner2);
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (mTask!=null)
			mTask.cancel(true);
	}

	private class ShowBGradeTask extends AsyncTask<Context, Void, Integer> {
		private Grade_Adapter rank_items;

		@Override
		protected void onPreExecute() {
			b_right.setVisibility(View.INVISIBLE);
			mProgbar.setVisibility(View.VISIBLE);
			lv.setAdapter(null);
			sp.setEnabled(false);
			spLeague.setEnabled(false);
		}

		protected Integer doInBackground(Context... ctx) {
			if ( isNoInternet())
				return comn_param.ERR_ID_NO_NETWORK;

			rank_items = new Grade_Adapter(ctx[0]);
			if ( mItem < 0 )
				return comn_param.ERR_ID_UNKNOWN;
			try {
				InputStream is = MyHTTPHelper.ConnectURL(String.format("http://www.cpbl.com.tw/stats/%s?page=1&pagecount=0&qyear=%d&qfield=%s", POS_WEBPAGE[mLeague][mPosition], comn_param.CPBL_YEAR, mItemStr));
				if ( is == null)
					return comn_param.ERR_ID_TIMEOUT;

				Parser_Grade bp = new Parser_Grade(is, mItem); 
				bp.stream.skip(8000);
				bp.gotoTagByProp("div", "id", "pal_view");
				bp.nextTag();	
				if (!bp.parsePlayerInfo(rank_items))
					return comn_param.ERR_ID_UNKNOWN;

				is.close();
				is = MyHTTPHelper.ConnectURL(String.format("http://www.cpbl.com.tw/stats/%s?page=%d&pagecount=0&qyear=%d&qfield=%s", POS_WEBPAGE[mLeague][mPosition], mPage, comn_param.CPBL_YEAR, mItemStr));
				if ( is == null)
					return comn_param.ERR_ID_TIMEOUT;

				bp.setInputStream(is);
				bp.stream.skip(8000);
				bp.gotoTagByProp("div", "id", "pal_view");
				bp.nextTag();
				if (!bp.parseQueryItem(rank_items))
					return comn_param.ERR_ID_UNKNOWN;
			}
			catch(Exception e)
			{
				e.printStackTrace();
				return comn_param.ERR_ID_UNKNOWN;
			}
			return comn_param.ID_SKIP;
		}

		protected void onPostExecute(Integer err) {
			mProgbar.setVisibility(View.INVISIBLE);
			b_right.setVisibility(View.VISIBLE);

			showDialogCompat(err);

			lv.setAdapter(rank_items);
			sp.setEnabled(true);
			spLeague.setEnabled(true);
		}
	}

	private class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
			loadAD();
			mItem = ARR_ITEM_OFFSET[mPosition][pos];
			mItemStr = ARR_ITEM_NAME[mPosition][pos];
			mPage = ARR_ITEM_PAGE[mPosition][pos];
		}

		public void onNothingSelected(AdapterView<?> parent) {
			loadAD();
		}
	}

	private class LeagueSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
			loadAD();
			mLeague = pos;
		}

		public void onNothingSelected(AdapterView<?> parent) {
			loadAD();
		}
	}
}
